package com.banma.web.util;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import com.banma.web.vo.JPushBean;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public class JPushClientUtil {
    static Logger logger = LoggerFactory.getLogger(JPushClientUtil.class);
    public static JPushBean jpushBean = null;
    public static JPushClient jpushClient = null;
    public static final String APP_KEY="8dfd58843df3e926e72a68a3";
    public static final String MASTER_SECRET="507b228cb0d097b664198d5a";
    /**
     * 发送通知
     *
     * @param registrationId
     *            设备标识
     * @param alert
     *            推送内容
     */
    public static void jSend_notification(String alert, Map<String, Object> map,
                                          String... alias) {
        logger.info(MASTER_SECRET);
        Gson gson = new Gson();
        jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, 3);
        try {
            jpushBean = new JPushBean();

            // 根据别名推送通知，不传默认推送全部
            if (alias.length >= 1) {
                jpushBean.setAlias(alias);
            }
            jpushBean.setNotification(alert, map);
            PushResult result = jpushClient
                    .sendPush(gson.toJson(jpushBean));
            logger.info(result.msg_id + "===" + result.sendno);

        } catch (APIConnectionException e) {
            logger.info(" e: {}", e);
        } catch (APIRequestException e) {
            logger.info(" e: {}", e);

        }
    }

    /**
     * 发送消息
     *
     * @param registrationId
     *            设备标识
     * @param alert
     *            推送内容
     */
    public static void jSend_Message(String alert, Map<String, Object> map, String... alias) {
        logger.info(MASTER_SECRET);
        Gson gson = new Gson();
        jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, 3);
        try {
            jpushBean = new JPushBean();
            // 根据别名推送通知，不传默认推送全部
            if (alias.length >= 1) {
                jpushBean.setAlias(alias);
            }
            jpushBean.setMessage(alert, map);
            PushResult result = jpushClient
                    .sendPush(gson.toJson(jpushBean));
            logger.info(result.msg_id + "===" + result.sendno);

        } catch (APIConnectionException e) {
            logger.info(" e: {}", e);
        } catch (APIRequestException e) {
            logger.info(" e: {}", e);

        }
    }

    public static void jSend_notification1(String masterSecret, String appKey,
                                           String content, String alert, Map<String, Object> map,
                                           String... alias) {

    }

    public static void main(String [] args){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("bizType","1");
        map.put("bizId","2");
        jSend_notification("您有新的询盘信息，请及时处理",map);
    }
}